//index.js
//获取应用实例
const app = getApp()
const util = require('../../../utils/util.js')

Page({
  data: {
    showFastLogin:false
  },
  toggleDialog: function (){
    this.setData({
      showFastLogin: !this.data.showFastLogin
    });
  },
  fromid:function(e){
    // console.log(e.detail)
    util.sendForm(e.detail.formId,'oa55NAX28JFUQxruYWzDfNq7PR617wRs36-9ByGQDuo','4')
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../phoneLogin/phone-login'
    })
  },
  bindFastLogin:function(){
    this.setData({
      showFastLogin: true
    });
  },
  closeFastLogin:function()
  {
    this.setData({
      showFastLogin: false
    });
  },
  onGetPhoneNumber:function(e){
    // console.log(app.globalData.code)
    if(e.detail.iv){
      // console.log(e.detail)
      wx.showLoading({
        title: '',
        mask: true,
        success: () => {
          // console.log(app.globalData.code)
          wx.request({
            url: app.globalData.baseUrl + '/weixin/getSessionKeyOropenid',
            data: {
              code: app.globalData.code,
            },
            method: 'post',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
              // console.log(res)
              if (res.data.code == '0') {
                wx.setStorageSync('session', res.data.data.session_key)
                wx.setStorageSync('openid', res.data.data.openid)
                wx.request({
                  url: app.globalData.baseUrl + '/weixin/getUserInfo',
                  data: {
                    openid: res.data.data.openid,
                    encryptedData: e.detail.encryptedData,
                    sessionKey: res.data.data.session_key,
                    iv: e.detail.iv
                  },
                  method: 'post',
                  header: {
                    'content-type': 'application/x-www-form-urlencoded'
                  },
                  success: function (response) {
                    wx.hideLoading()
                    // console.log(response.data)
                    if (response.data.code == '0') {
                      wx.setStorageSync('userInfo', response.data.data.data)
                      // console.log(util.shareFn)
                      util.shareFn()
                    } else {
                      // wx.showToast({
                      //   title: response.data.message,
                      //   image: '/images/register/cuowu@2x.png',
                      //   duration: 2000
                      // })
                      wx.navigateTo({
                        url: '../../unenter/unenter',
                      })
                    }
                  }
                })
              } else {
                wx.showToast({
                  title: res.data.message,
                  image: '/images/register/cuowu@2x.png',
                  duration: 2000
                })
              }
            }
          })
        }
      })
    }
  },
  onLoad: function (e) {
    util.getTimeFn()
    if (e.relauch){
      wx.login({
        success: res => {
          if (res.code) {
            app.globalData.code = res.code
            // setTimeout(() => {
            //   wx.redirectTo({
            //     url: '/pages/login/login/login',
            //   })
            // }, 1500)
          } else {
            // console.log(res)
          }
        }
      })
    }
  },
})
